import {Component, OnInit} from '@angular/core';
import {RechargeService} from '../../recharge.service';
import {ActivatedRoute} from '@angular/router';
import {NzMessageService} from 'ng-zorro-antd/message';
import {DatePipe} from '@angular/common';

@Component({
  selector: 'app-promotion-edit',
  templateUrl: './promotion-edit.component.html',
  styleUrls: ['./promotion-edit.component.scss']
})
export class PromotionEditComponent implements OnInit {
  formData = {
    id: null,
    type: 1,
    userType: 0,
    name: null,
    showCopy: null,
    remark: null,
    time: [] as any[],
    status: 5,
  };
  configTable: any[] = [];
  allDiscount = null;

  constructor(
    private rechargeService: RechargeService,
    private activatedRoute: ActivatedRoute,
    private nzMessageService: NzMessageService,
    private datePipe: DatePipe
  ) {
  }

  async ngOnInit() {
    await this.rechargeService.getMemberOptions().then((res: any) => {
      this.configTable = res.data.map((item: any) => ({...item, discount: null}));
    });
    const id = this.activatedRoute.snapshot.queryParams.id;
    const isCopy = this.activatedRoute.snapshot.queryParams.isCopy;
    if (id) {
      this.rechargeService.getPromotionDetail(id).then((res: any) => {
        console.log(res);
        if (res.code === 0) {
          this.formData.id = isCopy ? '' : res.data.promotionInfo.id;
          this.formData.type = res.data.promotionInfo.type;
          this.formData.userType = res.data.promotionInfo.userType;
          this.formData.name = res.data.promotionInfo.name;
          this.formData.showCopy = res.data.promotionInfo.showCopy;
          this.formData.remark = res.data.promotionInfo.remark;
          this.formData.time = [res.data.promotionInfo.beginTime, res.data.promotionInfo.endTime];
          this.formData.status = isCopy ? 5 : res.data.promotionInfo.status;
          this.configTable.forEach(item => {
            res.data.promotionInfoLog.forEach((r: any) => {
              if (r.id === item.id) {
                item.discount = r.discount;
                item.finalPrice = r.finalPrice;
                item.finalPriceB = r.finalPriceB;
              }
            });
          });
        }
      });
    }
  }

  typeChange() {
    this.configTable.forEach(item => {
      item.discount = null;
      item.finalPrice = null;
      item.finalPriceB = null;
    });
  }

  allDiscountChange() {
    this.configTable.forEach(item => {
      item.discount = this.allDiscount;
      // @ts-ignore
      this.discountChange(item, this.allDiscount);
    });
  }

  discountChange(data: any, value: number) {
    if (this.formData.type === 1) {
      // data.discount = data.price >= value ? data.discount : data.price;
      // data.discount = data.priceB >= value ? data.discount : data.priceB;
      data.finalPrice = data.price >= data.discount ? Math.floor((data.price - data.discount) * 100) / 100 : 0;
      data.finalPriceB = data.priceB >= data.discount ? Math.floor((data.priceB - data.discount) * 100) / 100 : 0;
    } else {
      data.finalPrice = Math.floor(data.price * value * 10) / 100;
      data.finalPriceB = Math.floor(data.priceB * value * 10) / 100;
    }
  }

  publish() {
    this.formData.status = 1;
    this.save();
  }

  save() {
    if (!this.formData.time.length) {
      this.nzMessageService.warning('请选择生效时间');
      return;
    }
    if (!this.formData.name) {
      this.nzMessageService.warning('请输入活动名称');
      return;
    }
    if (!this.formData.showCopy) {
      this.nzMessageService.warning('请输入前端标题');
      return;
    }
    this.rechargeService.editPromotion({
      id: this.formData.id,
      name: this.formData.name,
      type: this.formData.type,
      showCopy: this.formData.showCopy,
      beginTime: this.datePipe.transform(this.formData.time[0], 'yyyy-MM-dd HH:mm:ss'),
      endTime: this.datePipe.transform(this.formData.time[1], 'yyyy-MM-dd HH:mm:ss'),
      userType: this.formData.userType,
      remark: this.formData.remark,
      discountContentList: this.configTable.filter(item => item.discount).map((item) => ({id: item.id, discount: item.discount})),
      status: this.formData.status,
    }).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('操作成功');
        window.history.back();
      }
    });
  }

  publishP() {
    if (!this.formData.time.length) {
      this.nzMessageService.warning('请选择生效时间');
      return;
    }
    if (!this.formData.name) {
      this.nzMessageService.warning('请输入活动名称');
      return;
    }
    if (!this.formData.showCopy) {
      this.nzMessageService.warning('请输入前端标题');
      return;
    }
    this.rechargeService.editPromotion({
      id: this.formData.id,
      name: this.formData.name,
      type: this.formData.type,
      showCopy: this.formData.showCopy,
      beginTime: this.rechargeService.formatTime(this.formData.time[0], 'dateTime'),
      endTime: this.rechargeService.formatTime(this.formData.time[1], 'dateTime'),
      userType: this.formData.userType,
      remark: this.formData.remark,
      discountContentList: this.configTable.filter(item => item.discount).map((item) => ({id: item.id, discount: item.discount})),
      status: 1,
    }).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('操作成功');
        window.history.back();
      }
    });
  }
}
